
import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';

class DeviceBindUtils {

  static BuildContext? dlgContext;
  static Timer? _autoBackTimer;

  static void showConnectingDialog(deviceName) {
    if (Get.overlayContext != null) {
      showDialog(
          context: Get.overlayContext!,
          barrierDismissible: Platform.isIOS,
          builder: (BuildContext context) {
            dlgContext = context;
            return _buildConnectingDialog(
              context,
              deviceName,
              LocaleKeys.device_connecting.ts,
              'assets/images/device/device_waiting.png',
            );
          });
    }
  }

  static void showConnectedDialog(deviceName) {
    if (Get.overlayContext != null) {
      _autoBackTimer?.cancel();
      _autoBackTimer = Timer(const Duration(seconds: 3), () {
        dismissDialog();
        Get.back();
      });
      showDialog(
          context: Get.overlayContext!,
          barrierDismissible: Platform.isIOS,
          builder: (BuildContext context) {
            dlgContext = context;
            return _buildConnectedDialog(
              context,
              deviceName,
            );
          });
    }
  }

  static void dismissDialog() {
    _autoBackTimer?.cancel();
    if (dlgContext != null) {
      Navigator.of(Get.overlayContext!, rootNavigator: true).pop();
      dlgContext = null;
    }
  }

  static Widget _buildConnectingDialog(BuildContext context, title, message, icon) {
    return Material(
      type: MaterialType.transparency,
      child: PopScope(
        canPop: true,
        onPopInvoked: (didPop)=> {
          if (!didPop) {
            dismissDialog()
          }
        },
        child: Center(
          child: SizedBox(
            width: 916.w,
            height: 736.w,
            child: Container(
              decoration: const ShapeDecoration(
                color: Color(0xffffffff),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(20.0),
                  ),
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Text(title, style: TextStyle(fontSize: 54.sp)),
                  SizedBox(height: 97.h),
                  Image.asset(icon, width: 184.w, height: 204.h),
                  SizedBox(height: 126.h),
                  Text(message, style: TextStyle(fontSize: 54.sp)),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  static Widget _buildConnectedDialog(BuildContext context, title) {
    return Material(
      type: MaterialType.transparency,
      child: PopScope(
        canPop: true,
        onPopInvoked: (didPop)=> {
          if (!didPop) {
            dismissDialog()
          }
        },
        child: Center(
          child: SizedBox(
            width: 916.w,
            height: 756.w,
            child: Container(
              decoration: const ShapeDecoration(
                color: Color(0xffffffff),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(20.0),
                  ),
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(title, style: TextStyle(fontSize: 54.sp)),
                  SizedBox(height: 30.h),
                  Image.asset('assets/images/device/device_dialog_success.png', width: 222.w, height: 222.h),
                  SizedBox(height: 30.h),
                  Text(LocaleKeys.device_connected.ts, style: TextStyle(fontSize: 54.sp)),
                  SizedBox(height: 50.h),
                  SizedBox(
                    width: 806.w,
                    height: 145.w,
                    child: CupertinoButton(
                      color: ColorConst.accentColor,
                      onPressed: () {
                        dismissDialog();
                        Get.back();
                      },
                      child: Text(
                        LocaleKeys.back.ts,
                        style: TextStyle(color: Colors.white, fontSize: 42.sp),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}